package com.llb.llbliving.commodity.service.impl;

import com.llb.llbliving.commodity.dao.BrandDao;
import com.llb.llbliving.commodity.dao.CategoryDao;
import com.llb.llbliving.commodity.entity.BrandEntity;
import com.llb.llbliving.commodity.entity.CategoryEntity;
import com.llb.llbliving.commodity.service.BrandService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llb.common.utils.PageUtils;
import com.llb.common.utils.Query;

import com.llb.llbliving.commodity.dao.CategoryBrandRelationDao;
import com.llb.llbliving.commodity.entity.CategoryBrandRelationEntity;
import com.llb.llbliving.commodity.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Resource
    private BrandService brandService;

    //根据categoryId 返回该分类关联的品牌信息-集合
    @Override
    public List<BrandEntity> getBrandsByCategoryId(Long categoryId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities
                = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("category_id", categoryId));
//        List<Long> collect = categoryBrandRelationEntities.stream().map((obj) -> {
//            return obj.getBrandId();
//        }).collect(Collectors.toList());

        //List<BrandEntity> id = brandDao.selectList(new QueryWrapper<BrandEntity>().in("id", collect));

        List<BrandEntity> collect = categoryBrandRelationEntities.stream().map((obj) -> {
            Long brandId = obj.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAll(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        //1.获取到前端提交的brandId和categoryId
        Long brandId = categoryBrandRelationEntity.getBrandId();
        Long categoryId = categoryBrandRelationEntity.getCategoryId();

        //2.通过这两个id获取到各自的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);

        //3.将brandName 和categoryName设置到对象当中
        categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        categoryBrandRelationEntity.setCategoryName(categoryEntity.getName());

        //4.保存
        this.save(categoryBrandRelationEntity);
    }

}